#include "ViewEditor.h"

ViewEditor::ViewEditor() :
    tbPrevX(0),
    tbPrevY(0),
    shiftX(0),
    shiftY(0),
    rend(RenderManager::getInstance()),
    mainTool(ToolInvoker::getInstance()),
    globals(GlobalStates::getInstance()),
    ge(GameEngine::getInstance())
{
    startDrag=false;
}




View* ViewEditor::getInstance(){
    static View* inst=0;
    if(!inst)return inst=(View*)(new ViewEditor());
    else return inst;
}




void ViewEditor::resize(int width, int height){
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,2,240);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}




void ViewEditor::display(){
    static const GLfloat light_position[] = { 20.0f, 128.0f, 300.0f, 0.0f };

    glPushMatrix();

    glRotatef(rend->Camera.getRotY(),1,0,0);
    glRotatef(rend->Camera.getRotX(),0,1,0);
    glTranslatef(-rend->Camera.getEyeX(),-rend->Camera.getEyeY(),-rend->Camera.getEyeZ());

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    rend->Render();
    mainTool->invokeDisplay();
    ge->editorRender();

//    for (int i = 0; i<globals->selectionBoxes.size(); i++) {
//        Drawing::drawWireCuboid(globals->selectionBoxes[i].x, globals->selectionBoxes[i].y);
//    }
    glPopMatrix();

}




void ViewEditor::overlayDisplay(){
    mainTool->invokeOverlayDisplay();
}


void ViewEditor::init(){

}


void ViewEditor::destroy(){
    rend->Camera.reset();
}

void ViewEditor::rotHandler(float x, float y){
    float _x= (float)(globals->getScreenWidth() - x*2)/globals->getScreenWidth();
    float _y= (float)(globals->getScreenHeight() - y*2)/globals->getScreenHeight();

    _x = CLAMP(_x,-1.0,1.0);
    _y = CLAMP(_y,-1.0,1.0);

    float xAngle = asin(_x/1.0);
    float yAngle = asin(_y/1.0);
    float xDelta = TO_ANGLE(xAngle - tbPrevX);
    float yDelta = TO_ANGLE(yAngle - tbPrevY);

    rend->Camera.panRight(xDelta);
    rend->Camera.panDown(yDelta);

    tbPrevX = xAngle;
    tbPrevY = yAngle;

    //printf("\n%f %f\n",xAngle,yAngle);
}




void ViewEditor::resetRot(float x, float y){
    float _x= (float)(globals->getScreenWidth() - x*2)/globals->getScreenWidth();
    float _y= (float)(globals->getScreenHeight() - y*2)/globals->getScreenHeight();

    tbPrevX  = asin(_x/1.0);
    tbPrevY = asin(_y/1.0);

    startDrag=true;
}




Result ViewEditor::mousePressEvent(QMouseEvent *event){
    if(event->buttons()&Qt::MiddleButton && !startDrag)
        resetRot(event->x(),event->y());

    shiftX=event->x();
    shiftY=event->y();

    return mainTool->invokeMousePressEvent(event);
}




Result ViewEditor::mouseMoveEvent(QMouseEvent *event){

    if(event->buttons()&Qt::MiddleButton && startDrag)rotHandler(event->x(),event->y());
    if(event->buttons()&Qt::RightButton){
        //rend->Camera.setEyeX(rend->Camera.getEyeX()-(event->x()-shiftX)/5);
        //rend->Camera.setEyeY(rend->Camera.getEyeY()+(event->y()-shiftY)/5);
        rend->Camera.goLeft((event->x()-shiftX)/5);
        rend->Camera.goDown((event->y()-shiftY)/5);
    }

    shiftX=event->x();
    shiftY=event->y();

    return mainTool->invokeMouseMoveEvent(event);
}




Result ViewEditor::mouseReleaseEvent(QMouseEvent *event){
    startDrag=false;
    return mainTool->invokeMouseReleaseEvent(event);
}

Result ViewEditor::mouseEnterEvent(QEvent *event){
    return mainTool->invokeMouseEnterEvent(event);
}

Result ViewEditor::mouseLeaveEvent(QEvent *event){
    return mainTool->invokeMouseLeaveEvent(event);
}


Result ViewEditor::keyPressEvent(QKeyEvent *event){
    return mainTool->invokeKeyPressEvent(event);
}




Result ViewEditor::keyReleaseEvent(QKeyEvent *event){
    return mainTool->invokeKeyReleaseEvent(event);
}



Result ViewEditor::mouseWheelEvent(QWheelEvent *event){
    if(event->delta()<0)rend->Camera.goBack(5);
    else if(event->delta()>0)rend->Camera.goForward(5);
    return Result(true);
}

void ViewEditor::timerEvent(QTimerEvent *event){

}


